<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\response\Json;

class Level extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Level';

    //自定义方法排序
    protected array $sort = ['id' => 'asc'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'team_total', 'title' => '所需团队业绩', 'modify' => '2,number'])
            ->addColumn(['field' => 'tp', 'title' => '团队收益比例', 'modify' => '2,ratio', 'templet' => '{{(d.tp*100).toFixed(2)}}%'])
            ->addColumn(['field' => 'flat_tp', 'title' => '平级收益比例', 'modify' => '2,ratio', 'templet' => '{{(d.flat_tp*100).toFixed(2)}}%'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 80])
            ->addRightButtons(['edit' => ['size' => '50%']])
            ->fetch();
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        if (!empty($param['tp'])) {
            $param['tp'] = $param['tp'] / 100;
        }
        if (!empty($param['flat_tp'])) {
            $param['flat_tp'] = $param['flat_tp'] / 100;
        }

        return parent::save($param, $model);
    }
}